const config = require('./config.js')
import storage from '@/utils/storage.js'

const request = function (url, options) {
    options.data = { ...options.data, shop_name: uni.getStorageSync('domainname') || 'db' };
    let { JsonSuccessCode, JsonErrorCode, JsonShopExpiredCode, JsonCheckLoginCode, JsonTokenExpiredCode, JsonTokenErrorCode } = config.code || config.default.code;
    return new Promise((resolve, reject) => {
        uni.request({
            ...options,
            url: config.baseURL ? config.baseURL : config.default.baseURL + url,
            method: options.method,
            data: options.method.toUpperCase() == 'GET' ? options.data : JSON.stringify(options.data),
            header: {
                'Authorization': uni.getStorageSync('token') || 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJhdWQiOiIiLCJpYXQiOjE3MTYxODY3NTIsIm5iZiI6MTcxNjE4Njc2MiwiZXhwIjoxNzE2NzkxNTUyLCJkYXRhIjp7InVpZCI6MTk5fX0.aiV1sQtokVgONfM43sYrmv4i1wAjS75wenR-E9bGLY8' || null,
                'content-type': options.data && options.data.isUpload ? `multipart/form-data; boundary=----WebKitFormBoundary${generateString(26)}` : 'application/json',
                'domainname': options.data && options.data.shop_name,
            },
            success: (res) => {
                let { statusCode, data: { code } } = res;
                if (statusCode == 200) { 
                    if (code == JsonSuccessCode || code == JsonErrorCode) {
                        resolve(res.data);
                    } else {
                        switch (code) {
                            case JsonShopExpiredCode:
                                uni.showToast({ title: '店铺过期', icon: 'error' });
                                break;
                            case JsonCheckLoginCode:
                                uni.showToast({ title: '未登录', icon: 'error' });
                                break;
                            case JsonTokenExpiredCode:
                                uni.showToast({ title: 'token过期', icon: 'error' });
                                storage.clear();
                                break;
                            case JsonTokenErrorCode:
                                uni.showToast({ title: 'token解析失败', icon: 'error' });
                                storage.clear();
                                break;
                            default:
							
                                uni.showToast({ title: res.data.msg || '数据请求失败', icon: 'error' });
								resolve(res.data);
                                break;
                        }
                    }
                }
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}

function generateString(len = 6) {  // 生成随机数
    if (len <= 11) {
        return Math.random().toString(36).slice(2, 2 + len).padEnd(len, '0')
    } else {
        return generateString(11) + generateString(len - 11)
    }
}

module.exports = {
    get(url, data) {
        return request(url, {
            method: 'get',
            data
        })
    },
    post(url, data) {
        return request(url, {
            method: 'post',
            data
        })
    }
}